package com.supreme.cacheserver.controller;

import com.supreme.cacheserver.service.OmsDeptService;
import com.supreme.cacheserver.service.ReflectService;
import com.supreme.common.dto.OmsDept;
import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping(value = "oms")
public class OmsController {

    @Resource
    private ReflectService reflectService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private OmsDeptService omsDeptService;

    @GetMapping(value = "redisOmsDeptList/{key}")
    public AjaxResp redisOmsDeptList(@PathVariable(value = "key") String key) {
        List<OmsDept> omsDeptList = omsDeptService.getOmsDeptListFromRedis(key);

        if (Objects.nonNull(omsDeptList)) {
            CompletableFuture
                    .supplyAsync(() -> reflectService.showOmsDeptList(omsDeptList))
                    .thenAccept(totalConsumeTime -> System.out.println("Finished = " + totalConsumeTime));
        }

        return AjaxResp.instance(omsDeptList);
    }

    @GetMapping(value = "redisOmsDeptListByReflectField/{key}")
    public AjaxResp redisOmsDeptListByReflectField(@PathVariable(value = "key") String key) {
        List<Object> objectList = redisTemplate.opsForList().range(key, 0, -1);

        if (Objects.nonNull(objectList)) {
            CompletableFuture
                    .supplyAsync(() -> reflectService.showReflectFieldList(objectList, "com.supreme.common.dto.OmsDept"))
                    .thenAccept(totalConsumeTime -> System.out.println("Finished = " + totalConsumeTime));
        }

        return AjaxResp.instance(objectList);
    }
}
